package com.zc.study.thread.多线程第四章节.thread4_2_1;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Service {
    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    public void read() {
        System.out.println(Thread.currentThread().getName() + "获取读锁");
        lock.readLock().lock();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        lock.readLock().unlock();
        System.out.println(Thread.currentThread().getName()+"读锁释放");
    }
    public void write(){
        try {
            System.out.println(Thread.currentThread().getName()+"读取写锁");
            lock.writeLock().lock();
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.writeLock().unlock();
            System.out.println(Thread.currentThread().getName()+"释放写锁");
        }
    }
}
